/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ws1.exo3;

import com.ws1.exo3.domain.Address;
import com.ws1.exo3.domain.Customer;
import com.ws1.exo3.domain.Login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.LocalBean;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.sql.DataSource;

/**
 *
 * @author johann
 */
@Stateful
public class AccountCreatorService implements AccountCreatorServiceRemote {
    
    @Resource(name="jdbc/custDS")
    private DataSource ds;
    
    private Connection cn;
    private Customer customer;
    
    public AccountCreatorService() {
       
    }
    
     //méthodes de callback
    @PostConstruct
    @PostActivate //uniquement pour Stateful
    void init(){
        try {
            System.out.println("init_***************************");//cf console Glassfish / votre serveur d'application
            cn = ds.getConnection();//une instance Connection est assignée au stateful
        } catch (SQLException ex) {
            //cf. les journaux de log dans la console d'admin GF
            Logger.getLogger(AccountCreatorService.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex.getMessage()); //on relance une exception système 
            
        }
    }
    
    @PreDestroy
    @PrePassivate //pour SFSB uniquement
    void clear() {
        try {
            System.out.println("clear_***************************");
            cn.close();//libération de la connection
            cn=null;
        } catch (SQLException ex) {
            Logger.getLogger(AccountCreatorService.class.getName()).log(Level.SEVERE, null, ex);
            throw new EJBException(ex.getMessage()); //on relance une exception système 
        }
    }
    
    @Override
    public void addIdentityInfos(Customer customer) {
        System.out.println("addIdentity***************************");
        this.customer = customer;
    }

    @Override
    public void addLoginInfos(Login login) {
        System.out.println("addLogin_***************************");
        this.customer.setLogin(login);
    }

    @Override
    public void addAddress(Address address) {
        System.out.println("addAddress_***************************");
        this.customer.setAddress(address);
    }

    
    
    
    @Remove
    @Override
    public void createAccount() {
        System.out.println("createAccount_***************************");
        try {
            System.out.println("Insert VALUE...");

            String sql = "INSERT INTO CUSTOMERS (firstname,lastname,username,password,street,zipcode,city) "+
             " VALUES("+
              "'"+ customer.getFirstName() +"',"+
               "'lol',"+
                "'"+ customer.getLogin().getUserName() +"',"+
                 "'"+ customer.getLogin().getPassword() +"',"+
                  "'"+ customer.getAddress().getStreet() +"',"+
                   "'"+ customer.getAddress().getZipcode() +"',"+
                    "'"+ customer.getAddress().getCity() +"');";
            Statement stmt = cn.createStatement();
            stmt.execute(sql);
            stmt.close();
            
            System.out.println("...done!");
        } catch (SQLException ex) {
            Logger.getLogger(AccountCreatorService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    public Connection getCn() {
        return cn;
    }

    public void setCn(Connection cn) {
        this.cn = cn;
    }
    
    



}
